<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/19 0019
 * Time: 下午 6:28
 */
namespace app\article\controller\compose;
use app\article\validate\ValidateFun;
use app\article\validate\ValiReply;
use app\common\api\article\IReply;

class Reply implements IReply{
    private static $data=[];

    /**
     * 初始化
     * Reply constructor.
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        self::$data = array_filter_full(array_merge(self::$data,$config));
    }

    /**
     * 回复列表
     */
    public function reply_list()
    {
        $default = [
            'curr'=>1,
            'limits'=>10,
            'keyword'=>'',
        ];
        $data = array_filter_full(array_merge($default,self::$data));
        #获取数据
        $list = new \app\article\model\Reply();
        $res = $list->reply_list($data);
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    /**
     * 我的回复列表
     * @return array
     */
    public function myReply_list(){
        $admin = session('admin_info');
        $default = [
            'from_userid'=>$admin['id'],
            'curr'       =>1,
            'limits'     =>10,
        ];
        $data = array_filter_full(array_merge($default,self::$data));
        #验证参数
        $vali = ValiReply::myReply_list();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #获取数据
        $list = new \app\article\model\Reply();
        $res = $list->myReply_list($data);
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    #添加
    public function reply_add(){
        $admin = session('admin_info');
        $default_data =[
           'from_userid'=>$admin['id'],
           'createtime'=>date('Y-m-d H:i:s')
        ];
        $data = array_filter_full(array_merge($default_data,self::$data));
        unset($data['id']);
        #验证参数
        $vali = ValiReply::reply_add();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #添加回复
        $add = new \app\article\model\Reply();
        $res = $add ->reply_add($data);
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }
        return ['code'=>10006,'msg'=>config('msg.10006')];

    }
    #删除
    public function reply_update(){
        $data = self::$data;
        #验证参数
        $vali = ValiReply::reply_del();
        $validate=new ValidateFun($vali['rule'],$vali['message']);
        if(!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>"msg.$err"];
        }
        $del = new \app\article\model\Reply();
        $res = $del->reply_update($data);
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }
        return ['code'=>400,'msg'=>config('msg.400')];

    }
}